package org.laobai.meiqia.skill.mq;

import com.rabbitmq.client.Channel;
import org.laobai.meiqia.common.metas.OrderStatus;
import org.laobai.meiqia.common.util.JsonUtils;
import org.laobai.meiqia.skill.entity.SkillGoods;
import org.laobai.meiqia.skill.entity.SkillOrder;
import org.laobai.meiqia.skill.service.SkillGoodsService;
import org.laobai.meiqia.skill.service.SkillOrderService;
import org.laobai.meiqia.skill.service.impl.SkillGoodsServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Objects;

@Component
public class MeiQiaConsumer {
    @Autowired
    private SkillOrderService skillOrderService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private SkillGoodsService skillGoodsService;

    @RabbitListener(queues = "meiqia.skill.order")
    @RabbitHandler
    public void disposeSkillOrder(@Payload String msg, @Header(AmqpHeaders.DELIVERY_TAG) Long tagId, Channel channel) throws IOException {
        try {
            // 获取订单信息
            SkillOrder skillOrder = JsonUtils.toBean(msg, SkillOrder.class);

            //保存订单数据
            skillOrderService.saveSkillOrder(skillOrder);

            //发送消息到延时队列 fanout
            String orderNo = skillOrder.getOrderNo();
            MessageProperties messageProperties = new MessageProperties();
            rabbitTemplate.send("meiqia.skill.delay.order.exchange","",
                    new Message(orderNo.getBytes(),messageProperties));

            //恢复生产者
            channel.basicAck(tagId,false);
        }catch (Exception e){
            e.printStackTrace();
            // id 是否批量 是否入队
            channel.basicNack(tagId,false,false);
        }

    }

    @RabbitListener(queues = "meiqia.skill.dead.order")
    @RabbitHandler
    public void disposeDeadQueue(Message msg, @Header(AmqpHeaders.DELIVERY_TAG) Long tagId, Channel channel) throws IOException {
        String orderNO = new String(msg.getBody());
        System.out.println(orderNO);
        SkillOrder order = skillOrderService.getById(orderNO);

        if(Objects.nonNull(order)&&order.getStatus().equals(OrderStatus.WAIT_PAY.getCode())){
            order.setStatus(OrderStatus.TRADE_END.getCode());
            skillOrderService.updateById(order);
            // 回滚库存

            skillGoodsService.evalLua(order.getSkillId(), SkillGoodsServiceImpl.ROLLBACK_STOCK_LUA);
            // 确认消费
            channel.basicAck(tagId,false);
        }
    }
}
